/*
 * Copyright 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
 */
package proxy.Static;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Description:
 *
 * @author wzz
 * @since: 1.0
 * Create Date Time: 2018/12/14 10:15
 * Update Date Time:
 */
public class CglibProxyHandler implements MethodInterceptor {

    private Object target;
    public Object getProxyObject(Object target){
        this.target = target;
        // Enhancer类是CGLIB中的一个字节码增强器，它可以方便的对你想要处理的类进行扩展
        Enhancer enhancer = new Enhancer();
        // 将被代理的对象设置成父类
        enhancer.setSuperclass(this.target.getClass());
        // 回调方法，设置拦截器
        enhancer.setCallback(this);
        //  动态创建一个代理类
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("明星开始化妆");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("明星开始收钱");
        return result;
    }
}
